package cloud.maque.blog.impl;


import cloud.maque.blog.entity.MqComment;
import cloud.maque.blog.mapper.MqCommentMapper;
import cloud.maque.blog.service.MqCommentService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;

/**
 * 评论表;(mq_comment)表服务实现类
 *
 * @author : maque.cloud
 * @date : 2025-2-6
 */
@Service
public class MqCommentServiceImpl extends ServiceImpl<MqCommentMapper, MqComment> implements MqCommentService {


    /**
     * 通过ID查询单条数据
     *
     * @param commentId 主键
     * @return 实例对象
     */
    public MqComment queryById(String commentId) {
        return this.getById(commentId);
    }

    /**
     * 分页查询
     *
     * @param mqComment 筛选条件
     * @param current   当前页码
     * @param size      每页大小
     * @return
     */
    public IPage<MqComment> paginQuery(MqComment mqComment, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<MqComment> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(mqComment.getTenantId())) {
            queryWrapper.eq(MqComment::getTenantId, mqComment.getTenantId());
        }
        if (StrUtil.isNotBlank(mqComment.getCreatedBy())) {
            queryWrapper.eq(MqComment::getCreatedBy, mqComment.getCreatedBy());
        }
        if (StrUtil.isNotBlank(mqComment.getUpdatedBy())) {
            queryWrapper.eq(MqComment::getUpdatedBy, mqComment.getUpdatedBy());
        }
        if (StrUtil.isNotBlank(mqComment.getPostId())) {
            queryWrapper.eq(MqComment::getPostId, mqComment.getPostId());
        }
        if (StrUtil.isNotBlank(mqComment.getCommentContent())) {
            queryWrapper.eq(MqComment::getCommentContent, mqComment.getCommentContent());
        }
        if (StrUtil.isNotBlank(mqComment.getUserName())) {
            queryWrapper.eq(MqComment::getUserName, mqComment.getUserName());
        }
        if (StrUtil.isNotBlank(mqComment.getReplyId())) {
            queryWrapper.eq(MqComment::getReplyId, mqComment.getReplyId());
        }
        if (StrUtil.isNotBlank(mqComment.getFromWeb())) {
            queryWrapper.eq(MqComment::getFromWeb, mqComment.getFromWeb());
        }
        if (StrUtil.isNotBlank(mqComment.getFromUrl())) {
            queryWrapper.eq(MqComment::getFromUrl, mqComment.getFromUrl());
        }
        //2. 执行分页查询
        Page<MqComment> pagin = new Page<>(current, size, true);
        IPage<MqComment> selectResult = this.baseMapper.selectByPage(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return this.page(pagin, queryWrapper);
    }

    /**
     * 保存数据
     *
     * @param mqComment 实例对象
     * @return 实例对象
     */
    public MqComment Save(MqComment mqComment) {

        if (StrUtil.isEmptyIfStr(mqComment.getCommentId())) {
            this.baseMapper.insert(mqComment);
            return mqComment;
        }
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<MqComment> chainWrapper = new LambdaUpdateChainWrapper<MqComment>(this.baseMapper);
        if (StrUtil.isNotBlank(mqComment.getTenantId())) {
            chainWrapper.eq(MqComment::getTenantId, mqComment.getTenantId());
        }
        if (StrUtil.isNotBlank(mqComment.getCreatedBy())) {
            chainWrapper.eq(MqComment::getCreatedBy, mqComment.getCreatedBy());
        }
        if (StrUtil.isNotBlank(mqComment.getUpdatedBy())) {
            chainWrapper.eq(MqComment::getUpdatedBy, mqComment.getUpdatedBy());
        }
        if (StrUtil.isNotBlank(mqComment.getPostId())) {
            chainWrapper.eq(MqComment::getPostId, mqComment.getPostId());
        }
        if (StrUtil.isNotBlank(mqComment.getCommentContent())) {
            chainWrapper.eq(MqComment::getCommentContent, mqComment.getCommentContent());
        }
        if (StrUtil.isNotBlank(mqComment.getUserName())) {
            chainWrapper.eq(MqComment::getUserName, mqComment.getUserName());
        }
        if (StrUtil.isNotBlank(mqComment.getReplyId())) {
            chainWrapper.eq(MqComment::getReplyId, mqComment.getReplyId());
        }
        if (StrUtil.isNotBlank(mqComment.getFromWeb())) {
            chainWrapper.eq(MqComment::getFromWeb, mqComment.getFromWeb());
        }
        if (StrUtil.isNotBlank(mqComment.getFromUrl())) {
            chainWrapper.eq(MqComment::getFromUrl, mqComment.getFromUrl());
        }
        //2. 设置主键，并更新
        chainWrapper.set(MqComment::getCommentId, mqComment.getCommentId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if (ret) {
            return queryById(mqComment.getCommentId());
        } else {
            return mqComment;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param commentId 主键
     * @return 是否成功
     */
    public Boolean deleteById(String commentId) {
        int total = this.baseMapper.deleteById(commentId);
        return total > 0;
    }
}